/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class SDP_CONTENT implements Serializable{
    private String content_id;
    private String sdp_product_code;
    private String content_name;
    private String effective_time;
    private String expiry_time;
    private SDP_CONTENT_DETAILS[] listDetail;
    
    public SDP_CONTENT() {
    }

    public SDP_CONTENT(String sdp_product_code, String content_name, String effective_time, String expiry_time, SDP_CONTENT_DETAILS[] listDetail) {
        this.sdp_product_code = sdp_product_code;
        this.content_name = content_name;
        this.effective_time = effective_time;
        this.expiry_time = expiry_time;
        this.listDetail = listDetail;
    }

    public String getContent_id() {
        return content_id;
    }

    public void setContent_id(String content_id) {
        this.content_id = content_id;
    }

    public SDP_CONTENT_DETAILS[] getListDetail() {
        return listDetail;
    }

    public void setListDetail(SDP_CONTENT_DETAILS[] listDetail) {
        this.listDetail = listDetail;
    }


    public String getSdp_product_code() {
        return sdp_product_code;
    }

    public void setSdp_product_code(String sdp_product_code) {
        this.sdp_product_code = sdp_product_code;
    }

    public String getContent_name() {
        return content_name;
    }

    public void setContent_name(String content_name) {
        this.content_name = content_name;
    }    

    public String getEffective_time() {
        return effective_time;
    }

    public void setEffective_time(String effective_time) {
        this.effective_time = effective_time;
    }

    public String getExpiry_time() {
        return expiry_time;
    }

    public void setExpiry_time(String expiry_time) {
        this.expiry_time = expiry_time;
    }
    
    public String getProductIdByCode() throws Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "select product_id from sdp_products where sdp_product_code = '"+getSdp_product_code()+"'";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            if (rs.next()) {
                return rs.getString(1);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return "";
    }

    public void formatInput(){
        if(getEffective_time() == null){
             setEffective_time("");
        }
        if(getExpiry_time() == null){
            setExpiry_time("");
        }
    }
    
    public boolean insert() throws Exception{
        formatInput();
        Connection cn = null;
        Statement st = null;        
        try {
            cn = AppServer.getConnection();
            cn.setAutoCommit(false);
            st = cn.createStatement();            
            setContent_id(Database.getSequenceValue(cn, "seq_content"));
            String strSQL = "insert into sdp_contents(content_id,product_id,content_name,created_time,effective_time,expiry_time)"
                    + " values("+getContent_id()+","+getProductIdByCode()+",'"+getContent_name()+"',SYSDATE,to_date('"+getEffective_time()+"','dd-mm-yyyy'),to_date('"+getExpiry_time()+"','dd-mm-yyyy'))";
            if( st.executeUpdate(strSQL) > 0){
                if(insertDetails(cn)){
                    cn.commit();
                    return true;
                }
                return false;
            }
            return false;
        } finally {
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }        
    
    public boolean insertDetails(Connection cn) throws Exception{        
        Statement st = null;        
            st = cn.createStatement();            
            for(int i=0;i<listDetail.length;i++){
                String strSQL = "insert into sdp_content_details(detail_id,content_id,content,content_index,status,product_content_type)"
                        + " values(seq_content.nextval,"+getContent_id()+",'"+listDetail[i].getContent()+"',"+listDetail[i].getContent_index()+","+listDetail[i].getStatus()+",'"+listDetail[i].getProduct_content_type()+"')";
                if(st.executeUpdate(strSQL) < 1) return false;            
            }
            return true;
    }
}
